package com.ruoyi.ldzlsb.controller;

import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.ldzlsb.domain.SbRepairOrder;
import com.ruoyi.ldzlsb.service.ISbRepairOrderService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.ldzlsb.domain.vo.SbEquipmentVo;
import com.ruoyi.ldzlsb.service.ISbEquipmentService;
import com.ruoyi.common.core.utils.SpringUtils;

/**
 * 设备维修单Controller
 * 
 * @author ruoyi
 * @date 2025-07-25
 */
@RestController
@RequestMapping("/sb/repair/order")
public class SbRepairOrderController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(SbRepairOrderController.class);

    @Autowired
    private ISbRepairOrderService sbRepairOrderService;

    /**
     * 查询维修单列表
     */
    @GetMapping("/findAll")
    public TableDataInfo findAll(SbRepairOrder sbRepairOrder)
    {
        startPage();
        List<SbRepairOrder> list = sbRepairOrderService.selectSbRepairOrderList(sbRepairOrder);
        return getDataTable(list);
    }

    /**
     * 导出维修单列表
     */
    @Log(title = "设备维修单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SbRepairOrder sbRepairOrder)
    {
        List<SbRepairOrder> list = sbRepairOrderService.selectSbRepairOrderList(sbRepairOrder);
        ExcelUtil<SbRepairOrder> util = new ExcelUtil<SbRepairOrder>(SbRepairOrder.class);
        util.exportExcel(response, list, "维修单数据");
    }

    /**
     * 获取维修单详细信息
     */
    @GetMapping("/findById")
    public AjaxResult getInfo(@RequestParam("order_id") Long repairId)
    {
        return success(sbRepairOrderService.selectSbRepairOrderByRepairId(repairId));
    }

    /**
     * 新增维修单
     */
    @Log(title = "设备维修单", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult add(@RequestBody SbRepairOrder sbRepairOrder)
    {
        return toAjax(sbRepairOrderService.insertSbRepairOrder(sbRepairOrder));
    }

    /**
     * 修改维修单
     */
    @Log(title = "设备维修单", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody SbRepairOrder sbRepairOrder)
    {
        return toAjax(sbRepairOrderService.updateSbRepairOrder(sbRepairOrder));
    }

    /**
     * 删除维修单
     */
    @Log(title = "设备维修单", businessType = BusinessType.DELETE)
    @GetMapping("/delete")
    public AjaxResult remove(@RequestParam("order_id") Long repairId)
    {
        return toAjax(sbRepairOrderService.deleteSbRepairOrderByRepairIds(new Long[]{repairId}));
    }

    /**
     * 根据设备ID查询维修单列表
     */
    @GetMapping("/equipment/{equipmentId}")
    public AjaxResult getByEquipmentId(@PathVariable Long equipmentId)
    {
        List<SbRepairOrder> list = sbRepairOrderService.selectSbRepairOrderByEquipmentId(equipmentId);
        return success(list);
    }

    /**
     * 生成维修单号
     */
    @GetMapping("/generateCode")
    public AjaxResult generateRepairCode()
    {
        String repairCode = sbRepairOrderService.generateRepairCode();
        return success(repairCode);
    }

    /**
     * 完成维修
     */
    @Log(title = "维修完成", businessType = BusinessType.UPDATE)
    @PostMapping("/complete")
    public AjaxResult complete(@RequestBody SbRepairOrder sbRepairOrder)
    {
        log.info("收到完成维修请求，维修单ID: {}, 状态: {}", sbRepairOrder.getRepairId(), sbRepairOrder.getStatus());
        int result = sbRepairOrderService.completeSbRepairOrder(sbRepairOrder);
        log.info("完成维修处理结果: {}", result);
        return toAjax(result);
    }

    /**
     * 取消维修
     */
    @Log(title = "取消维修", businessType = BusinessType.UPDATE)
    @GetMapping("/cancel")
    public AjaxResult cancel(@RequestParam("order_id") Long repairId, @RequestParam("cancel_reason") String reason)
    {
        return toAjax(sbRepairOrderService.cancelSbRepairOrder(repairId, reason));
    }

    /**
     * 开始维修
     */
    @Log(title = "开始维修", businessType = BusinessType.UPDATE)
    @PostMapping("/start")
    public AjaxResult start(@RequestBody SbRepairOrder sbRepairOrder)
    {
        return toAjax(sbRepairOrderService.handleSbRepairOrder(sbRepairOrder));
    }

    /**
     * 获取可用设备列表
     */
    @GetMapping("/equipment/list")
    public TableDataInfo getEquipmentList()
    {
        try {
            // 调用设备服务获取可用设备
            SbEquipmentVo query = new SbEquipmentVo();
            query.setEquipmentStatus("0"); // 0表示正常状态
            
            // 自动注入设备Service
            ISbEquipmentService equipmentService = SpringUtils.getBean(ISbEquipmentService.class);
            if (equipmentService != null) {
                startPage();
                List<SbEquipmentVo> list = equipmentService.selectSbEquipmentList(query);
                return getDataTable(list);
            } else {
                return getDataTable(new ArrayList<>());
            }
        } catch (Exception e) {
            log.error("获取设备列表失败", e);
            return getDataTable(new ArrayList<>());
        }
    }
} 